//
//  MyOrder.m
//  YouthHostel
//
//  Created by administrator on 16/10/12.
//  Copyright © 2016年 Lorain mac pro. All rights reserved.
//

#import "MyOrder.h"
#import "Define.h"
#import "MyorderCell.h"
#import "NotCheckInOrder.h"
#import "AppDelegate.h"
#import "HistoryOrderCell.h"
#import "Judge.h"
#import "UIImageView+WebCache.h"
#import "DateTimeViewController.h"
@interface MyOrder ()<UIScrollViewDelegate,UITableViewDelegate,UITableViewDataSource>
//导航栏
@property (strong,nonatomic)UIView *nav;
//标题
@property (strong,nonatomic)UILabel *titleLab;

@property (strong,nonatomic)UITableView *myTab;
//未入住
@property (strong,nonatomic)UIButton *notCheckIn;
//已入住
@property (strong,nonatomic)UIButton *alreadyCheckIn;
//分割线
@property (strong,nonatomic)UILabel *lineLab1;
@property (strong,nonatomic)UILabel *slidingLab;

@property (strong,nonatomic)UIScrollView *myScroll;

@property (strong,nonatomic)UITableView *orderTab;

@property (strong,nonatomic)UITableView *historyTab;

@property (strong,nonatomic)HistoryOrderCell *histortCell;

@property (strong,nonatomic)NSMutableArray *nameArr;
@property (strong,nonatomic)NSMutableArray *inTimeArr;
@property (strong,nonatomic)NSMutableArray *leaveArr;
@property (strong,nonatomic)NSMutableArray *peaceArr;
@property (strong,nonatomic)NSMutableArray *imgArr;
@property (strong,nonatomic)UIImageView *img;
@property (strong,nonatomic)UILabel *lab;
@property (strong,nonatomic)UIImageView *img1;
@property (strong,nonatomic)UILabel *lab1;
@property (strong,nonatomic)UIView *viewbg;
@property (strong,nonatomic)UIView *viewbg1;


@end

@implementation MyOrder

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];

    self.nav = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 64)];
    self.nav.backgroundColor = [UIColor colorWithRed:56/255.0 green:143/255.0 blue:214/255.0 alpha:1];
    
    self.titleLab = [[UILabel alloc]initWithFrame:CGRectMake(100, 20, WIDTH-200, 34)];
    self.titleLab.textAlignment = NSTextAlignmentCenter;
    self.titleLab.text = @"我的订单";
    self.titleLab.textColor = [UIColor whiteColor];
    self.titleLab.font = [UIFont systemFontOfSize:20];
    
    
    self.notCheckIn = [[UIButton alloc]initWithFrame:CGRectMake(0, 64, WIDTH/2, 50)];
    self.notCheckIn.titleLabel.font = [UIFont boldSystemFontOfSize:18];
    [self.notCheckIn setTitleColor:[UIColor colorWithRed:56/255.0 green:143/255.0 blue:214/255.0 alpha:1] forState:UIControlStateNormal];
    self.notCheckIn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [self.notCheckIn setTitle:@"未入住" forState:UIControlStateNormal];
    [self.notCheckIn addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventTouchUpInside];
    self.notCheckIn.tag = 1001;
    
    self.alreadyCheckIn = [[UIButton alloc]initWithFrame:CGRectMake(WIDTH/2, 64, WIDTH/2, 50)];
    self.alreadyCheckIn.titleLabel.font = [UIFont systemFontOfSize:15];
    self.alreadyCheckIn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [self.alreadyCheckIn setTitle:@"已入住" forState:UIControlStateNormal];
    [self.alreadyCheckIn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.alreadyCheckIn addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventTouchUpInside];
    self.alreadyCheckIn.tag = 1002;

    self.myScroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 114, WIDTH, HEIGHT-114-49)];
    self.myScroll.contentSize = CGSizeMake(WIDTH*2, HEIGHT-114);
    self.myScroll.delegate = self;
    self.myScroll.bounces = NO;
    self.myScroll.showsHorizontalScrollIndicator = NO;
    self.myScroll.pagingEnabled = YES;
    self.myScroll.scrollEnabled = NO;
    
    self.viewbg = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-114)];
    self.viewbg.backgroundColor = [UIColor whiteColor];
    
    self.img = [[UIImageView alloc]initWithFrame:CGRectMake(WIDTH/2-30, HEIGHT/2-150, 60, 60)];
    self.img.image = [UIImage imageNamed:@"订单信息.png"];
    
    self.lab = [[UILabel alloc]initWithFrame:CGRectMake(50, HEIGHT/2-80, WIDTH-100, 30)];
    self.lab.text = @"您还没有订单哦";
    self.lab.textAlignment = NSTextAlignmentCenter;
    self.lab.textColor = [UIColor lightGrayColor];

    self.viewbg1 = [[UIView alloc]initWithFrame:CGRectMake(WIDTH, 0, WIDTH, HEIGHT-114)];
    self.viewbg1.backgroundColor = [UIColor whiteColor];
    
    self.img1 = [[UIImageView alloc]initWithFrame:CGRectMake(WIDTH/2-30, HEIGHT/2-150, 60, 60)];
    self.img1.image = [UIImage imageNamed:@"订单信息.png"];
    
    self.lab1 = [[UILabel alloc]initWithFrame:CGRectMake(50, HEIGHT/2-80, WIDTH-100, 30)];
    self.lab1.text = @"您还没有订单哦";
    self.lab1.textAlignment = NSTextAlignmentCenter;
    self.lab1.textColor = [UIColor lightGrayColor];
    
    self.orderTab = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-114-49) style:UITableViewStylePlain];
    self.orderTab.delegate = self;
    self.orderTab.dataSource = self;
    self.orderTab.showsVerticalScrollIndicator = NO;
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.orderTab.alwaysBounceVertical = NO;
    self.orderTab.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.historyTab = [[UITableView alloc]initWithFrame:CGRectMake(WIDTH, 0, WIDTH, HEIGHT-114-49) style:UITableViewStylePlain];
    self.historyTab.delegate = self;
    self.historyTab.dataSource = self;
//    self.historyTab.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.historyTab.showsVerticalScrollIndicator = NO;
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.historyTab.alwaysBounceVertical = NO;
    self.historyTab.separatorStyle = UITableViewCellSeparatorStyleNone;

    
    self.slidingLab = [[UILabel alloc]initWithFrame:CGRectMake(0, 112, WIDTH/2, 2)];
    self.slidingLab.backgroundColor = [UIColor colorWithRed:56/255.0 green:143/255.0 blue:214/255.0 alpha:1];

    
    self.lineLab1 = [[UILabel alloc]initWithFrame:CGRectMake(WIDTH, 114, WIDTH, 1)];

    [self.view addSubview:_nav];
    [self.nav addSubview:_titleLab];
    [self.view addSubview:_notCheckIn];
    [self.view addSubview:_alreadyCheckIn];
    [self.view addSubview:_myScroll];
    [self.view addSubview:_lineLab1];
    [self.view addSubview:_slidingLab];
    
    [self.myScroll addSubview:_orderTab];
    [self.myScroll addSubview:_historyTab];
    
    [self.myScroll addSubview:_viewbg];
    [_viewbg addSubview:_img];
    [_viewbg addSubview:_lab];
    
    [self.myScroll addSubview:_viewbg1];
    [_viewbg1 addSubview:_img1];
    [_viewbg1 addSubview:_lab1];
    
    [self creat];
    
    NSLog(@"%@",NSHomeDirectory());
}


//创建数据源
- (void)creat{
    
    //数组的初始化
    _inTimeArr = [NSMutableArray array];
    _leaveArr = [NSMutableArray array];
    _peaceArr = [NSMutableArray array];
    _nameArr = [NSMutableArray array];
    _imgArr = [NSMutableArray array];
    
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString *userid = [user objectForKey:@"user_id"];
    
    NSMutableDictionary *mutDic = [NSMutableDictionary dictionaryWithCapacity:0];
    [mutDic setObject:userid forKey:@"user"];
    
    Define *server = [[Define alloc]init];
    [server searchMessageUrl:NOTINORDER andDic:mutDic andSuccess:^(NSDictionary *dic) {
        
        NSLog(@"%@",dic);
        
        NSArray *arr = [dic objectForKey:@"data"];
        
        for (int i = 0 ; i < arr.count; i++) {
            NSDictionary *dic = arr[i];
            NSString *name = [dic objectForKey:@"hotel_name"];
            NSString *inTime = [dic objectForKey:@"notorder_time"];
            NSString *img = [dic objectForKey:@"room_image"];
            
            [_nameArr addObject:name];
            [_inTimeArr addObject:inTime];
            [_imgArr addObject:img];
            
        }
        if (_nameArr.count == 0) {
            _viewbg.hidden = NO;
            _viewbg1.hidden = NO;
        }else{
            _viewbg.hidden = YES;
            _viewbg1.hidden = YES;
        }
        
    }];

}



//跳转到评价页
-(void)getJudge:(UIButton *)sender{

    [self hiddenTabBar];
     Judge *vc = [[Judge alloc]init];
     [self.navigationController pushViewController:vc animated:YES];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [self creat];
    [_orderTab reloadData];
    [_historyTab reloadData];
    AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
    [appDelegate.selfTAB showTabView];
    
    [self.tabBarController.tabBar setHidden:NO];
}


//隐藏TabBar
- (void)hiddenTabBar{
    AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
    [appDelegate.selfTAB hideTabView];
    
    [self.tabBarController.tabBar setHidden:YES];
    
}


- (void)back:(UIButton *)sender{
    [self.navigationController popViewControllerAnimated:YES];
}


-(UIButton *)buttonWithTag:(NSInteger)tag{
    if (tag==1001) {
        return _notCheckIn;
    }else if (tag==1002){
        return _alreadyCheckIn;
    }else{
        return nil;
    }
}

-(void)sliderAction:(UIButton *)sender{
    [self sliderAnimationWithTag:sender.tag];
    [UIView animateWithDuration:0.3 animations:^{
        self.myScroll.contentOffset = CGPointMake(WIDTH * (sender.tag - 1001), 0);
    } completion:^(BOOL finished) {
        
    }];
    
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    double index_ = self.myScroll.contentOffset.x / WIDTH;
    [self sliderAnimationWithTag:(int)(index_+0.5)+1001];
}

- (void)sliderAnimationWithTag:(NSInteger)tag{
    self.notCheckIn.selected = NO;
    self.alreadyCheckIn.selected = NO;
    UIButton *sender = [self buttonWithTag:tag];
    sender.selected = YES;
    //动画
    [UIView animateWithDuration:0.3 animations:^{
        self.slidingLab.frame = CGRectMake(sender.frame.origin.x, self.slidingLab.frame.origin.y, self.slidingLab.frame.size.width, self.slidingLab.frame.size.height);
        
    } completion:^(BOOL finished) {
        self.notCheckIn.titleLabel.font = [UIFont systemFontOfSize:15];
        [self.notCheckIn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        //
        self.alreadyCheckIn.titleLabel.font = [UIFont systemFontOfSize:15];
        [self.alreadyCheckIn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        
        sender.titleLabel.font = [UIFont boldSystemFontOfSize:18];
        [sender setTitleColor:[UIColor colorWithRed:56/255.0 green:143/255.0 blue:214/255.0 alpha:1] forState:0];
    }];
    
    
    
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    
    if (tableView == _orderTab) {
        [self hiddenTabBar];
        NotCheckInOrder *vc = [[NotCheckInOrder alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
    }else{
        
        
    }
    
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == _orderTab) {
        
        if (_nameArr.count != 0) {
            return _nameArr.count;
        }else{
            return 1;
    }
    }else{
        
        return 1;
        
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == _orderTab) {
        return 180;
    }else{
    return 180;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == _orderTab) {
        MyorderCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        
        if (!cell) {
            cell = [[MyorderCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        }
        
        if (_nameArr.count != 0) {

        [cell.hotelImg sd_setImageWithURL:_imgArr[indexPath.row]];
        
        cell.namelab.text = _nameArr[indexPath.row];
        
        NSString *time1 = [NSString stringWithFormat:@"预约时间： %@",_inTimeArr[indexPath.row]];
        cell.timeLab.text = time1;
        
        }
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        return cell;

    }else{
         self.histortCell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        
        if (!self.histortCell) {
            self.histortCell = [[HistoryOrderCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        }
        self.histortCell.hotelImg.image = [UIImage imageNamed:@"room1.jpg"];
        self.histortCell.nameLab.text = @"香格里拉3001房";
        self.histortCell.alreadyLab.text = @"2016-9-1 20:00   入住";
        self.histortCell.leaveLab.text = @"2016-9-1 20:00   离开";
        self.histortCell.peaceLab.text = @"总价:  600";
        self.histortCell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        [self.histortCell.judgeBtn addTarget:self action:@selector(getJudge:) forControlEvents:UIControlEventTouchUpInside];
        
        return self.histortCell;
    }
    
    }

#pragma mark --删除或添加cell
//编辑的风格
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == _historyTab) {
        //返回删除效果
        return UITableViewCellEditingStyleDelete;
        
    }else{
        return 0;
    }
}
//显示的右边删除button的title
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"确定删除";
}
//提交编辑效果
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        //当你删除时
        NSLog(@"第%lu行被删除了",indexPath.row);
        //先将数据源中的数据删除了
        [_nameArr removeObjectAtIndex:indexPath.row];
        //以动画的形式删除指定的cell
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight];
        
    }
    else if (editingStyle == UITableViewCellEditingStyleInsert){
        
        NSLog(@"第%lu行被插入了",indexPath.row);
        //现在数据源里插入一个元素
        [_nameArr insertObject:@"添加的新行" atIndex:indexPath.row];
        //以动画的形式增加指定的元素
        [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
    }
}

#pragma mark --移动cell
//是否移动
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    //指定代理方法  可以移动cell
    return YES;
}

//移动代理方法
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    NSLog(@"移动");
    //取出要移动的元素
    NSString *item = [_nameArr objectAtIndex:sourceIndexPath.row];
    //    删除原有的元素
    [_nameArr removeObjectAtIndex:sourceIndexPath.row];
    //将源位置数据插入目的位置
    [_nameArr insertObject:item atIndex:destinationIndexPath.row];
    
}



@end
